home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / DMPWRLD.C < prev    next >
C/C++ Source or Header  |  1990-11-28  |  6KB  |  255 lines

  1. /* -*-C-*-
  2.  
  3. $Header: dmpwrld.c,v 9.36 90/11/28 22:43:02 GMT cph Exp $
  4.  
  5. Copyright (c) 1987, 1988, 1989, 1990 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* This file contains a primitive to dump an executable version of Scheme.
  36.    It uses unexec.c from GNU Emacs.
  37.    Look at unexec.c for more information. */
  38.  
  39. #include "scheme.h"
  40. #include "prims.h"
  41.  
  42. #ifndef unix
  43. #include "Error: dumpworld.c does not work on non-unix machines."
  44. #endif
  45.  
  46. #include "ux.h"
  47. #include "osfs.h"
  48. #include <sys/file.h>
  49.  
  50. /* Compatibility definitions for GNU Emacs's unexec.c.
  51.    Taken from the various m-*.h and s-*.h files for GNU Emacs.
  52. */
  53.  
  54. #define CANNOT_UNEXEC
  55.  
  56. #if defined (vax)
  57. #undef CANNOT_UNEXEC
  58. #endif
  59.  
  60. #if defined (hp9000s300)
  61. #undef CANNOT_UNEXEC
  62. #define ADJUST_EXEC_HEADER                           \
  63.   hdr.a_magic = ((ohdr.a_magic.file_type == OLDMAGIC.file_type) ?    \
  64.          NEWMAGIC : ohdr.a_magic);
  65. #endif
  66.  
  67. #if defined (hp9000s800)
  68. #undef CANNOT_UNEXEC
  69. #endif
  70.  
  71. #if defined (sun3)
  72. #undef CANNOT_UNEXEC
  73. #define SEGMENT_MASK        (SEGSIZ - 1)
  74. #define A_TEXT_OFFSET(HDR)    sizeof (HDR)
  75. #define TEXT_START        (PAGSIZ + (sizeof(struct exec)))
  76. #endif
  77.  
  78. /* I haven't tried any below this point. */
  79.  
  80. #if defined (umax)
  81. #undef CANNOT_UNEXEC
  82. #define HAVE_GETPAGESIZE
  83. #define COFF
  84. #define UMAX
  85. #define SECTION_ALIGNMENT    pagemask
  86. #define SEGMENT_MASK        (64 * 1024 - 1)
  87. #endif
  88.  
  89. #if defined (celerity)
  90. #undef CANNOT_UNEXEC
  91. #endif
  92.  
  93. #if defined (sun2)
  94. #undef CANNOT_UNEXEC
  95. #define SEGMENT_MASK        (SEGSIZ - 1)
  96. #endif
  97.  
  98. #if defined (pyr)
  99. #undef CANNOT_UNEXEC
  100. #define SEGMENT_MASK (2048-1)    /* ZMAGIC format */
  101.                 /* man a.out for info */
  102. #endif
  103.  
  104. #ifdef CANNOT_UNEXEC
  105. #include "Error: dmpwrld.c only works on a few machines."
  106. #endif
  107.  
  108. #ifndef TEXT_START
  109. #define TEXT_START    0
  110. #endif
  111.  
  112. #ifndef SEGMENT_MASK
  113. #define DATA_START    (&etext)
  114. #else
  115. #define DATA_START    \
  116. (((((unsigned) &etext) - 1) & ~SEGMENT_MASK) + (SEGMENT_MASK + 1))
  117. #endif
  118.  
  119. #if defined (_HPUX)
  120. #define USG
  121. #define HPUX
  122. #endif
  123.  
  124. /* More compatibility definitions for unexec. */
  125.  
  126. extern int end, etext, edata;
  127.  
  128. char
  129. *start_of_text()
  130. {
  131.   return ((char *) TEXT_START);
  132. }
  133.  
  134. char
  135. *start_of_data()
  136. {
  137.   return ((char *) DATA_START);
  138. }
  139.  
  140. #if defined (USG) || defined (NO_BZERO)
  141.  
  142. #define bzero(b,len)    (memset((b), 0, (len)))
  143.  
  144. #else
  145.  
  146. extern void bzero();
  147.  
  148. #endif
  149.  
  150. #define static
  151.  
  152. #if defined (hp9000s800)
  153. #include "unexhp9k800.c"
  154. #else
  155. #include "unexec.c"
  156. #endif
  157.  
  158. #undef static
  159.  
  160. void
  161. DEFUN (unix_find_pathname, (program_name, target),
  162.        CONST char * program_name AND char * target)
  163. {
  164.   int length;
  165.   char
  166.     * path,
  167.     * next;
  168.   extern char *
  169.     EXFUN (index, (char * path AND char srchr));
  170.   extern void
  171.     EXFUN (strcpy, (char * target AND CONST char * source));
  172.  
  173.   /* Attempt first in the connected directory */
  174.  
  175.   if (((program_name[0]) == '/')
  176.       || (OS_file_access (program_name, X_OK))
  177.       || ((path = ((char *) (getenv ("PATH")))) == ((char *) NULL)))
  178.   {
  179.     strcpy (target, program_name);
  180.     return;
  181.   }
  182.   for (next = (index (path, ':'));
  183.        path != ((char *)  NULL);
  184.        path = (next + 1),
  185.        next = (index (path, ':')))
  186.   {
  187.     length = ((next == ((char *) NULL))
  188.           ? (strlen (path))
  189.           : (next-path));
  190.     strncpy (target, path, length);
  191.     target[length] = '/';
  192.     target[length + 1] = '\0';
  193.     strcpy ((target + (length + 1)), program_name);
  194.     if (OS_file_access (target, X_OK))
  195.     {
  196.       return;
  197.     }
  198.   }
  199.   strcpy (target, program_name);
  200.   return;
  201. }
  202.  
  203. /* The primitive visible from Scheme. */
  204.  
  205. extern Boolean scheme_dumped_p;
  206.  
  207. DEFINE_PRIMITIVE ("DUMP-WORLD", Prim_dump_world, 1, 1, 0)
  208. {
  209.   int result;
  210.   SCHEME_OBJECT arg;
  211.   Boolean saved_dumped_p;
  212.   char
  213.     * fname,
  214.     path_buffer[FILE_NAME_LENGTH];
  215.   PRIMITIVE_HEADER (1);
  216.  
  217.   PRIMITIVE_CANONICALIZE_CONTEXT();
  218.  
  219.   arg = (ARG_REF (1));
  220.   fname = (STRING_ARG (1));
  221.  
  222.   /* Set up for restore */
  223.  
  224.   saved_dumped_p = scheme_dumped_p;
  225.  
  226.   scheme_dumped_p = true;
  227.   Val = SHARP_T;
  228.   POP_PRIMITIVE_FRAME (1);
  229.  
  230.   /* Dump! */
  231.  
  232.   unix_find_pathname (scheme_program_name, path_buffer);
  233.   result = (unexec (fname,
  234.             path_buffer,
  235.             ((unsigned) 0),        /* default */
  236.             ((unsigned) 0),        /* default */
  237.             ((unsigned) start_of_text())));
  238.  
  239.   /* Restore State */
  240.  
  241.   Val = SHARP_F;
  242.   scheme_dumped_p = saved_dumped_p;
  243.  
  244.   /* IO: Restoring cached input for this job. */
  245.  
  246.   if (result != 0)
  247.   {
  248.     STACK_PUSH (arg);
  249.     error_external_return ();
  250.   }
  251.  
  252.   PRIMITIVE_ABORT (PRIM_POP_RETURN);
  253.   /*NOTREACHED*/
  254. }
  255.